<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%cms_article_audio}}".
 *
 * @property int $id
 * @property int $article_id
 * @property string|null $path
 * @property int $attach_id
 * @property string|null $description
 * @property string|null $reader
 * @property int $duration
 * @property int $size
 * @property int $listorder
 * @property int $created_at
 * @property int $updated_at
 */
class CmsArticleAudio2 extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_article_audio}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['article_id', 'attach_id', 'duration', 'size', 'listorder', 'created_at', 'updated_at'], 'integer'],
            [['path', 'description'], 'string', 'max' => 255],
            [['reader'], 'string', 'max' => 80],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'article_id' => 'Article ID',
            'path' => 'Path',
            'attach_id' => 'Attach ID',
            'description' => 'Description',
            'reader' => 'Reader',
            'duration' => 'Duration',
            'size' => 'Size',
            'listorder' => 'Listorder',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getThumbUrl() {
        return Url::attach($this->thumb);
    }

    public function getPathUrl() {
        return Url::video($this->path);
    }

    public function getPublishTime() {
        $time = $this->created_at;
        return $time ? date('Y-m-d', $time) : date('Y-m-d');
    }

    public function getDurationTime() {
        return gmdate('H:i:s', $this->duration);
    }

    public function getAudio() {
        return [
            'reader' => $this->reader,
            'duration' => $this->durationTime,
            'path' => $this->pathUrl,
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'reader',
            'duration' => 'durationTime',
            'path' => 'pathUrl',
        ];
    }

}
